<!-- xuwencheng  2022年6年29日18:00  这是我的wenzhangbianji -->
<!-- 编辑 -->
<template>
  <el-dialog
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :destory-on-close="true"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="65%"
    @close="close"
  >
    <el-form ref="form" label-width="200px" :model="form" :rules="rules">
      <el-form-item label="剧组姓名" prop="crewName">
        <el-input v-model="form.crewName" placeholder="请输入剧组姓名" />
      </el-form-item>
      <el-form-item label="剧组类别" prop="crewClass">
        <el-select v-model="form.crewClass" placeholder="请选择剧组类别">
          <el-option
            v-for="item in options1"
            :key="item.id"
            :label="item.creClasName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="剧组资质文件" prop="identifyArray">
        <el-upload
          v-model="form.identifyArray"
          action="https://star.gengduoke.com/adminapi/file/upload"
          :headers="{
            'X-Token': token,
          }"
          :limit="3"
          list-type="picture-card"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="dialogImageUrl"
            alt=""
            :src="dialogImageUrl"
            width="100%"
          />
          <i v-else class="el-icon-plus"></i>
        </el-upload>

        <el-dialog :visible.sync="dialogVisible">
          <img alt="" :src="dialogImageUrl" width="100%" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="form.startTime"
          placeholder="选择日期"
          type="date"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker
          v-model="form.endTime"
          placeholder="选择日期"
          type="date"
        />
      </el-form-item>
      <el-form-item label="剧组题材" prop="crewTheme">
        <el-input v-model="form.crewTheme" />
      </el-form-item>
      <el-form-item label="拍摄时间" prop="crewTime">
        <el-input v-model="form.crewTime" />
      </el-form-item>
      <el-form-item label="故事梗概" prop="storyTheme">
        <el-input v-model="form.storyTheme" />
      </el-form-item>
      <el-form-item label="导演姓名" prop="director">
        <el-input v-model="form.director" />
      </el-form-item>
      <!-- <el-form-item label="选择城市" prop="cityId">
        <el-cascader
          v-model="form.cityId"
          :options="options"
          style="width: 100%"
          @change="addressChoose"
        />
      </el-form-item> -->
      <el-form-item label="区域" prop="areaId">
        <el-select v-model="form.areaId" placeholder="请选择区域">
          <el-option
            v-for="item in options2"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
            @click.native="chooseone(item)"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="主演名称" prop="protagonist">
        <el-input v-model="form.protagonist" />
      </el-form-item>
    </el-form>
    <el-button class="baocun" size="medium" type="primary" @click="save">
      保存
    </el-button>
  </el-dialog>

  <!-- 增加 -->
</template>

<script>
  /* eslint-disable*/
  import { doAdd, doEdit, crewClass, areaId, crewtheme } from '@/api/jvzu'

  import { geturl } from '@/api/lunbotu'

  import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
  } from 'element-china-area-data'
  //import { number } from 'echarts'
  export default {
    name: 'search',

    data() {
      const validatesurfacePlot = (rule, value, callback) => {
        console.log(this.form.identifyArray)
        if (this.form.identifyArray === '') {
          callback(new Error('请添加图片!!!!'))
        } else {
          callback()
        }
      }
      return {
        dialogImageUrl: '',
        dialogVisible: false,
        options: regionDataPlus,
        selectedOptions: [],
        token: '',
        imageUrl: '',
        value: true,
        value1: true,

        dialogVisible: false,
        disabled: false,
        options1: [],
        options2: [],
        options3: [],
        valueone: '',
        valuetwo: '',
        form: {
          cityId: '', //chengshiid
          sort: 0,
          roles: [],
          displayLocked: true,
          recommendLocked: true,
          share: true,
          locked: false,
          crewThemeId: '', //剧组题材id
          surfacePlot: '',
          crewTheme: '', //剧组题材
          cityName: '', //城市名
          areaName: '', //区域名称
          areaId: '', //区域id
          identifyArray: [],
        },
        activated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.play()
        },
        deactivated() {
          if (this.$refs.MaskVideo.src) this.$refs.MaskVideo.pause()
        },

        rules: {
          identifyArray: [
            { validator: validatesurfacePlot, trigger: 'change' },
          ],
          crewName: [
            { required: true, trigger: 'blur', message: '请输入剧组姓名' },
          ],
          crewClass: [
            { required: true, trigger: 'change', message: '请选择剧组类别' },
          ],
          crewTheme: [
            { required: true, trigger: 'change', message: '请输入剧组题材' },
          ],
          crewTime: [
            { required: true, trigger: 'blur', message: '请输入拍摄时间' },
          ],
          storyTheme: [
            { required: true, message: '请输入故事梗概', trigger: 'blur' },
          ],
          director: [
            { required: true, trigger: 'blur', message: '请输入导演姓名' },
          ],
          // cityId: [
          //   { required: true, trigger: 'change', message: '请选择城市' },
          // ],
          areaId: [
            { required: true, trigger: 'change', message: '请选择区域' },
          ],
          crewAddress: [
            { required: true, trigger: 'blur', message: '请选择城市' },
          ],
          endTime: [
            { required: true, trigger: 'change', message: '请选择结束时间' },
          ],
          startTime: [
            { required: true, trigger: 'change', message: '请选择开始时间' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.fetchData()
      this.fetchDatatwo()
      this.fetchDatathree()
      // console.log(22222)
      this.token = this.$store.getters['user/token']
      // console.log(this.token)
    },
    methods: {
      handleRemove(file, fileList) {
        console.log('fle', file, 'fileList', fileList)
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      choose(e) {
        this.form.crewTheme = e.themeName
        this.form.crewThemeId = e.id
      }, //选择区域
      chooseone(e) {
        this.form.areaName = e.areaName
        this.form.areaId = e.id
      },
      handleChange(a) {
        this.form.particulars = a
      },

      handleAvatarSuccess(res, file, fileList) {
        this.form.identifyArray.push(res.data)

        console.log(this.form.identifyArray)
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg'
        const isLt2M = file.size / 1024 / 1024 < 2
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!')
        }
        return true
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          // console.log(row.identifyArray)
          this.dialogImageUrl = row.identifyArray.map((item) => {
            this.dialogImageUrl = item
            console.log(this.dialogImageUrl)
            this.geturls()
          })
        }
        this.dialogFormVisible = true
      },
      async geturls() {
        const res = await geturl({
          f: this.dialogImageUrl,
        })

        this.dialogImageUrl = res
        console.log(this.dialogImageUrl)
      },
      close() {
        let userId = this.form.userId
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        if (!this.form.id) {
          this.form.userId = userId
        }
        console.log(this.form)
        this.dialogFormVisible = false
        // this.imageUrl = ''
        // this.form = ''
      },
      async save() {
        // this.$router.push('/other/workflow')

        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.id) {
              const msg = await doEdit(this.form)
              console.log('21321', msg)
              this.$baseMessage(msg.msg, 'success', 'vab-hey-message-success')
              this.$emit('fetch-data')
              this.close()
            } else {
              console.log(this.form.identifyArray)
              const msg = doAdd(this.form)

              this.$emit('fetch-data')
              this.close()
              msg.then((res) => {
                console.log(res.msg)
                if (res.msg === '添加成功') {
                  this.$baseMessage(
                    res.msg,
                    'success',
                    'vab-hey-message-success'
                  )
                  this.$router.push({ path: '/mall/goods' })
                }
              })
            }
          }
        })
      },
      async fetchData() {
        let queryForm = {}
        const res = await crewClass(queryForm)
        this.options1 = res.data
      },

      async fetchDatatwo() {
        let queryForm = {}
        const res = await areaId(queryForm)
        this.options2 = res.data
      },
      async fetchDatathree() {
        let that = this
        let queryForm = {}
        const res = await crewtheme(queryForm)
        this.options3 = res.data
      },
      nickNameChange() {
        this.$forceUpdate()
      },
      /**省市区三级联动 */
      addressChoose(value) {
        console.log(value)

        this.form.cityId = value[2]
        this.form.cityName =
          CodeToText[value[0]] +
          '/' +
          CodeToText[value[1]] +
          '/' +
          CodeToText[value[2]]
      },
    },
  }
</script>
<style>
  /* .huakuai {
    position: relative;
    left: 96%;
  } */
  .avatar-uploader .el-upload {
    position: relative;
    overflow: hidden;
    cursor: pointer;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    width: 178px;
    height: 178px;
    font-size: 28px;
    line-height: 178px;
    color: #8c939d;
    text-align: center;
  }
  .avatar {
    display: block;
    width: 178px;
    height: 178px;
  }
  .baocun {
    width: 100%;
    height: 100%;
  }
</style>
